<?php

namespace Controller\Ajax;

class Passport extends Base {

    /**
     * 用户注册.
     */
    public function register() {
        $business = \Business\User::instance();
        $email = $this->postParam('email', true);
		$password = $this->postParam('password');
        $ip = $this->getIp();
        try {
            // 调业务层的注册
            $business->register($email, $password, $ip);
            $this->response(true);
        } catch (\Lib\Exception\Business $e) {
            $this->responseError($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 首页上的注册
     */
    public function registerIndex() {
        $mask = $this->getUserMask();
        $business = \Business\User::instance();
        $email = $this->postParam('email', true);
        $username = $this->postParam('dogName', true);
        $password = $this->postParam('password');
        $breed = $this->postParam('breed', true);
        $verify = $this->postParam('verify', true);
        $sex = $this->postInt('sex');
        $ip = $this->getIp();
        try {
            // 调业务层的注册
            $business->registerIndex($mask, $email, $username, $password, $breed, $verify, $sex, $ip);
            $this->response(true);
        } catch (\Lib\Exception\Business $e) {
            $this->responseError($e->getMessage(), $e->getCode());
        }
    }

	public function registerOrLoginBindOAuth() {
		$business = \Business\User::instance();
		$email = $this->postParam('email', true);
		$username = $this->postParam('username', true);
		$password = $this->postParam('password');
		$mask = $this->getUserMask();
		$ip = $this->getIp();
		try {
			// 调业务层的注册
			$business->registerOrLoginBindOAuth($mask, $email, $password, $username, $ip);
			$this->response(true);
		} catch (\Lib\Exception\Business $e) {
			$this->responseError($e->getMessage(), $e->getCode());
		}
	}

    public function login() {
        $business = \Business\User::instance();
        $mask = $this->getUserMask();
        $email = $this->postParam('email', null);
        $password = $this->postParam('password', null);
        $verify = $this->postParam('verify');
        $ip = $this->getIp();
        try {
            $business->login($email, $password, $mask, $ip, $verify);
            $this->response(true);
        } catch (\Lib\Exception\Business $e) {
            $this->responseError($e->getMessage(),$e->getCode());
        }
    }

    public function setTrueNameAndSex() {
        $id = $this->currentUserInfo['Uid'];
        $trueName = $this->postParam('trueName', true);
        $sex = $this->postParam('sex');
        try {
            \Business\User::instance()->setTrueNameAndSexByUid($id, $trueName, $sex);
            $this->response('完善成功');
        } catch (\Lib\Exception\Business $e) {
            $this->responseError($e->getMessage(), $e->getCode());
        }
    }

    public function loginOut(){
        $mask = $this->getUserMask();
        try {
            \Business\User::instance()->loginOut($mask);
            $this->response(true);
        } catch (\Lib\Exception\Business $e){
            $this->responseError($e->getMessage());
        }
    }

}